﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DestinationOperations;
using System.Text.RegularExpressions;

namespace Beginning
{
    public partial class frmAddWayStation : Form
    {
        public long id
        {
            get;
            set;
        }

        public frmAddWayStation(long id)
        {
            this.id = id;
            InitializeComponent();
            SettlementsBindControls();
        }
                
        private void SettlementsBindControls()
        {
            var settlements = from s in DAO.GetAllSettlements()
                              orderby s.SettlementName
                              select s.SettlementName;

            cbWayStation.DataSource = settlements.ToList();

            var destSettlements = from s in DAO.GetAllWayStationForDestination(id)                                  
                                  select s.Settlement.SettlementName;

            cbAfterWayStation.DataSource = destSettlements.ToList();

            //var data = from s in DAO.GetAllWayStationForDestination(id)
            //           where s.WayStationID == cbAfterWayStation.Text
            //           select s.s

            //lblNewDistTo.Text = "Distance to\n" + DAO.GetTheLastWayStationForDestination(id);
            //lblNewPriceTo.Text = "Price to\n" + DAO.GetTheLastWayStationForDestination(id);
            //lblNewTimeTo.Text = "Time to\n" + DAO.GetTheLastWayStationForDestination(id);

        }

        private long AfterWayStationID(string settlement, long destID)
        {
            var wayStationID = from ws in DAO.GetAllWayStationForDestination((int)destID)
                               where ws.Settlement.SettlementName == settlement
                               select ws.WayStationID;

            return wayStationID.First();
        }

        private long GetSettlementID(string name)
        {
            return DAO.GetSettlementID(name);
        }

        private void btnAddWayStation_Click(object sender, EventArgs e)
        {
            bool confirm =
                MessageBox.Show("Add this way station?", "Confirmation", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes;

            if (confirm)
            {
                DAO.InsertWayStation(GetSettlementID(cbWayStation.Text), id, Convert.ToInt32(txtDistTo.Text),
                    ((controlTimeTo.Hours * 60) + (controlTimeTo.Minutes)), Convert.ToInt32(txtStay.Text),
                    Convert.ToDecimal(txtPriceTo.Text), AfterWayStationID(cbAfterWayStation.Text, id),
                    Convert.ToInt32(txtNewDistTo.Text), ((controlNewTimeTo.Hours * 60) + (controlNewTimeTo.Minutes)),
                    Convert.ToDecimal(txtNewPriceTo.Text));

                MessageBox.Show("The way station - " + cbWayStation.Text + " added!");
            }
        }

        private void btnAddSettlement_Click(object sender, EventArgs e)
        {
            gbNewSettlement.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            gbNewSettlement.Enabled = false;
        }

        private void btnInsertSettlement_Click(object sender, EventArgs e)
        {
            bool confirmation =
                MessageBox.Show("A new settlement added!", "Message",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK;

            if (confirmation)
            {
                DAO.AddSettlement(txtNewSettlementName.Text);
                SettlementsBindControls();
                gbNewSettlement.Visible = false;
            }
        }

        private void cbWayStation_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblDistTo.Text = "Distance to\n" + cbWayStation.Text;
            lblPriceTo.Text = "Price to\n" + cbWayStation.Text;
            lblTimeTo.Text = "Time to\n" + cbWayStation.Text;
        }

        private void cbAfterWayStation_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            frmDestinationDetails main = new frmDestinationDetails(id);
            main.Show();
            this.Hide();
        }


        #region Validation

        private void txtDistTo_Validating(object sender, CancelEventArgs e)
        {
            ValidateDistTo();
        }

        private void txtPriceTo_Validating(object sender, CancelEventArgs e)
        {
            ValidatePriceTo();
        }

        private void txtNewDistTo_Validating(object sender, CancelEventArgs e)
        {
            ValidateNewDistTo();
        }

        private void txtNewPriceTo_Validating(object sender, CancelEventArgs e)
        {
            ValidateNewPriceTo();
        }

        private void txtStay_Validating(object sender, CancelEventArgs e)
        {
            ValidateStay();
        }

        private bool ValidateDistTo()
        {
            if (IsDistToValid(txtDistTo.Text))
            {
                epValidateData.SetError(txtDistTo, "");
                return true;
            }
            else
            {
                epValidateData.SetError(txtDistTo, "Not a valid distance!");
                return false;
            }
        }

        private bool ValidateNewDistTo()
        {
            if (IsDistToValid(txtNewDistTo.Text))
            {
                epValidateData.SetError(txtNewDistTo, "");
                return true;
            }
            else
            {
                epValidateData.SetError(txtNewDistTo, "Not a valid distance!");
                return false;
            }
        }

        private bool IsDistToValid(string distance)
        {
            try
            {
                if (Int32.Parse(distance) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (FormatException)
            {
                return false;
            }
        }

        private bool ValidatePriceTo()
        {
            if (IsPriceToValid(txtPriceTo.Text))
            {
                epValidateData.SetError(txtPriceTo, "");
                return true;
            }
            else
            {
                epValidateData.SetError(txtPriceTo, "Not a valid price!");
                return false;
            }
        }

        private bool ValidateNewPriceTo()
        {
            if (IsPriceToValid(txtNewPriceTo.Text))
            {
                epValidateData.SetError(txtNewPriceTo, "");
                return true;
            }
            else
            {
                epValidateData.SetError(txtNewPriceTo, "Not a valid price!");
                return false;
            }
        }

        private bool ValidateStay()
        {
            if (IsPriceToValid(txtStay.Text))
            {
                epValidateData.SetError(txtStay, "");
                return true;
            }
            else
            {
                epValidateData.SetError(txtStay, "Not valid minutes for stay!");
                return false;
            }
        }

        private bool IsPriceToValid(string price)
        {
            try
            {
                if (Decimal.Parse(price) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (FormatException)
            {
                return false;
            }
        }

        private bool ValidateNewSettlement()
        {
            if (IsNewSettlementValid(txtNewSettlementName.Text))
            {
                epValidateData.SetError(txtNewSettlementName, "");
                return true;
            }
            else
            {
                epValidateData.SetError(txtNewSettlementName, "Not a valid name format!");
                return true;
            }
        }

        private bool IsNewSettlementValid(string settlement)
        {
            string pattern = @"[A-Z]{1}[a-z]+";
            Regex regex = new Regex(pattern);
            Match match = regex.Match(settlement);

            if (match.Success)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
    }
}
